package br.com.artsy.controller;

import java.awt.Image;
import java.io.IOException;
import java.util.Calendar;

import javax.imageio.ImageIO;

import br.com.artsy.util.ImageUtils;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.interceptor.multipart.UploadedFile;

@Resource
public class IndexController {
	private final Result result;

	public IndexController(Result result) {
		this.result = result;
	}

	@Path("/")
	public void index() {

	}

	@Path("files/add")
	public void uploadImage(UploadedFile uploadedFile, String width, String height) throws IOException {
		if (uploadedFile != null) {

//			BufferedImage source = ImageIO.read(uploadedFile.getFile());
			int destWidth = new Integer(width).intValue();
			int destHeight = new Integer(height).intValue();

			 Image imageUpload = ImageIO.read(uploadedFile.getFile());
			 ImageUtils.saveImage(ImageUtils.resizeImage(imageUpload,
			 ImageUtils.IMAGE_JPEG, destWidth, destHeight), "D://img_"+Calendar.getInstance().getTimeInMillis()+".jpg",
			 ImageUtils.IMAGE_JPEG);
			 
			 result.redirectTo("index");
		}
	}
}
